package com.crt.nexus.oauth.authorization;

import com.crt.nexus.core.spring.ClassScanner;
import com.crt.nexus.security.annotation.GrantType;
import com.google.common.collect.Maps;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;

import java.util.Map;

@Component
public class AuthenticationProcessor implements ApplicationRunner {

    private final Map<String, Class<?>> map = Maps.newHashMapWithExpectedSize(10);

    Class<?> get(String type) {
        return map.get(type);
    }

    private void init() {
        ClassScanner.scan(ClassUtils.getPackageName(AuthenticationProcessor.class), GrantType.class).forEach(clazz -> {
            String type = clazz.getAnnotation(GrantType.class).value();
            map.put(type, clazz);
        });
    }

    @Override
    public void run(ApplicationArguments args) {
        this.init();
    }

}
